# Trick Candles
# Copyright (C) 2024  Jeffry Johnston
#
# This file is part of Trick Candles.
#
# Trick Candles is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Trick Candles is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Trick Candles.  If not, see <https://www.gnu.org/licenses/>.

name := Candles
version := 01.11

headers :=			\
 	src/candles.h		\

srcs :=				\
	display.c		\
	game.c			\
	kbd.c			\
	main.c			\

images :=			\
	candle1.png		\
	candle2.png		\
	candle3.png		\
	candle4.png		\
	candle5.png		\
	candle6.png		\
	candle7.png		\
	flame.png		\
	gameover.png		\
	help.png		\
	melted.png		\
	score.png		\
	sel_candle1.png		\
	sel_candle2.png		\
	sel_candle3.png		\
	sel_candle4.png		\
	sel_candle5.png		\
	sel_candle6.png		\
	sel_candle7.png		\
	wick.png		\
	font_birthday.png	\

icon := assets/icon.png

FX_CC := sh-elf-gcc
FX_CFLAGS := -DFX9860G -DTARGET_FX9860G -m3 -mb -ffreestanding -nostdlib \
	-Wa,--dsp -Wall -Wextra -g -Os -fstrict-volatile-bitfields
FX_LDFLAGS := -nostdlib -Wl,--no-warn-rwx-segments -T fx9860g.ld
fx_add_in := build_fx/$(name).g1a
fx_bin := build_fx/$(name).bin
fx_elf := build_fx/$(name).elf
fx_objs := $(srcs:%=build_fx/%.o) $(images:%=build_fx/%.o)
fx_libs := $(shell $(FX_CC) -print-file-name=libgint-fx.a) \
	$(shell $(FX_CC) -print-file-name=libc.a) -lm -lgint-fx -lopenlibm -lc -lgcc
fx_icon := assets/icon.png

.PHONY: all
all: fx fxg3a

.PHONY: fx
fx: $(fx_add_in)

$(fx_add_in): $(fx_bin) $(fx_icon)
	fxgxa --g1a -n $(name) -i $(fx_icon) --version="$(version)" -o $@ $<

$(fx_bin): $(fx_elf)
	sh-elf-objcopy -O binary -R .bss -R .gint_bss $< $@

$(fx_elf): $(fx_objs)
	$(FX_CC) $(FX_LDFLAGS) -o $@ $^ $(fx_libs) $(fx_libs)

build_fx/%.c.o: src/%.c $(headers)
	$(FX_CC) $(FX_CFLAGS) -c -o $@ $<

build_fx/%.png.o: assets/%.png assets/fxconv-metadata.txt
	fxconv --toolchain=sh-elf --fx -o $@ $<

FXG3A_CC := sh-elf-gcc
FXG3A_CFLAGS := -DFXCG50 -DFX9860G_G3A -m4-nofpu -mb -ffreestanding -nostdlib \
	-Wa,--dsp -Wall -Wextra -g -Os -fstrict-volatile-bitfields
FXG3A_LDFLAGS := -nostdlib -Wl,--no-warn-rwx-segments -T fxcg50.ld
fxg3a_add_in := build_fxg3a/$(name).g3a
fxg3a_bin := build_fxg3a/$(name).bin
fxg3a_elf := build_fxg3a/$(name).elf
fxg3a_objs := $(srcs:%=build_fxg3a/%.o) $(images:%=build_fxg3a/%.o)
fxg3a_libs := $(shell $(FXG3A_CC) -print-file-name=libgint-fxg3a.a) \
	$(shell $(FXG3A_CC) -print-file-name=libc.a) -lm -lgint-fxg3a -lopenlibm -lc -lgcc
fxg3a_icon_uns := assets/icon_uns.png
fxg3a_icon_sel := assets/icon_sel.png

.PHONY: fxg3a
fxg3a: $(fxg3a_add_in)

$(fxg3a_add_in): $(fxg3a_bin) $(fxg3a_icon_uns) $(fxg3a_icon_sel)
	fxgxa --g3a -n $(name) --icon-uns $(fxg3a_icon_uns) --icon-sel $(fxg3a_icon_sel) --version="$(version)" -o $@ $<

$(fxg3a_bin): $(fxg3a_elf)
	sh-elf-objcopy -O binary -R .bss -R .gint_bss $< $@

$(fxg3a_elf): $(fxg3a_objs)
	$(FXG3A_CC) $(FXG3A_LDFLAGS) -o $@ $^ $(fxg3a_libs) $(fxg3a_libs)

build_fxg3a/%.c.o: src/%.c $(headers)
	$(FXG3A_CC) $(FXG3A_CFLAGS) -c -o $@ $<

build_fxg3a/%.png.o: assets/%.png assets/fxconv-metadata.txt
	fxconv --toolchain=sh-elf --fx -o $@ $<

$(shell mkdir -p build_fx build_fxg3a)

.PHONY: bss
bss:
	size --common -t build_fx/*.o build_fx/$(name).g1a

# Install on Casio fx-9750/9860 GIII
.PHONY: install
install: $(fx_add_in)
	@while true; do \
	    DEVICE=`blkid | grep LABEL_FATBOOT=\"CASIO\" | cut -d':' -f1`; \
	    [ -n "$$DEVICE" ] && break; \
	    sleep 0.25; \
	done; \
	while true; do \
	    DEST=`findmnt -nr -o target $$DEVICE`; \
	    [ -n "$$DEST" ] && break; \
	    sleep 0.25; \
	done; \
	rm -f "$$DEST/"$(name).g1a; \
	cp -v $(fx_add_in) "$$DEST"/; \
	eject "$$DEST" 2>/dev/null || true

# Install on Casio fx-CG50
.PHONY: cg
cg: $(fxg3a_add_in)
	@while true; do \
	    DEVICE=`blkid | grep LABEL_FATBOOT=\"CASIO\" | cut -d':' -f1`; \
	    [ -n "$$DEVICE" ] && break; \
	    sleep 0.25; \
	done; \
	while true; do \
	    DEST=`findmnt -nr -o target $$DEVICE`; \
	    [ -n "$$DEST" ] && break; \
	    sleep 0.25; \
	done; \
	rm -f "$$DEST/"$(name).g3a; \
	cp -v $(fxg3a_add_in) "$$DEST"/; \
	eject "$$DEST" 2>/dev/null || true

# Install on Casio fx-9750/9860 G/GII
.PHONY: p7
p7: $(fx_add_in)
	p7 send $(fx_add_in)

.PHONY: clean
clean:
	$(RM) -r build_fx/ build_fxg3a/
